package com.oracle.oa.action;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.oracle.oa.biz.ManualSignBiz;
import com.oracle.oa.entity.Manualsign;
import com.oracle.oa.entity.Userinfo;

public class DoSelect extends ActionSupport implements SessionAware {

	public String begintime;
	public String endtime;
	public String alltime;
	public String range;
	public String department;
	public int userid;
	public String username;
	
	ManualSignBiz msbiz;
	
	Map<String,Object> session;
	
	@Override
	public String execute() throws Exception {
		
		Userinfo user=(Userinfo) msbiz.getById(Userinfo.class, Integer.parseInt(session.get("userid").toString()));
		
		//获取系统当前时间
		Timestamp now=new Timestamp(System.currentTimeMillis());
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sfd=new SimpleDateFormat("yyyy-MM");
		SimpleDateFormat sfy=new SimpleDateFormat("yyyy");
		
		int week=java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_WEEK);
		String btime="";
		String etime="";
		switch(week){
		case 1:
			btime=sf.format(now.getTime()-6*1000*60*60*24);
			etime=sf.format(now.getTime()+1*1000*60*60*24);
			break;
		case 2:
			btime=sf.format(now);
			etime=sf.format(now.getTime()+7*1000*60*60*24);
			break;
		case 3:
			btime=sf.format(now.getTime()-1*1000*60*60*24);
			etime=sf.format(now.getTime()+6*1000*60*60*24);
			break;
		case 4:
			btime=sf.format(now.getTime()-2*1000*60*60*24);
			etime=sf.format(now.getTime()+5*1000*60*60*24);
			break;
		case 5:
			btime=sf.format(now.getTime()-3*1000*60*60*24);
			etime=sf.format(now.getTime()+4*1000*60*60*24);
			break;
		case 6:
			btime=sf.format(now.getTime()-4*1000*60*60*24);
			etime=sf.format(now.getTime()+3*1000*60*60*24);
			break;
		case 7:
			btime=sf.format(now.getTime()-5*1000*60*60*24);
			etime=sf.format(now.getTime()+2*1000*60*60*24);
			break;
		}
		
		String day=sf.format(now);
		String month=sfd.format(now);
		String year=sfy.format(now);
		
		//判断时间范围
		if(((begintime!=null&&begintime.length()!=0)&&(endtime!=null&&endtime.length()!=0)&&alltime==null)||((begintime!=null&&begintime.length()!=0)&&(endtime!=null&&endtime.length()!=0)&&alltime!=null)){
			List<Manualsign> slmslist=null;
			
			if(sf.parse(endtime).getTime()<sf.parse(begintime).getTime()){
				String temp=begintime;
				begintime=endtime;
				endtime=temp;
			}
			
			if(department!=null){
				if(user.getRoleinfo().getRoleid()==1){
					slmslist=msbiz.getByTimeAndDept(begintime, endtime, department);
				}else if(user.getRoleinfo().getRoleid()==2){
					slmslist=msbiz.getPgByTime(user, begintime, endtime);
				}else if(user.getRoleinfo().getRoleid()==3){
					slmslist=msbiz.getPuByTime(user, begintime, endtime);
				}
				
			}else if(userid!=0){
				
				slmslist=msbiz.getByIdAndTime(begintime, endtime, userid);
			}else if(username!=null){
				
				slmslist=msbiz.getByUnameAndTime(begintime, endtime, username);
			}else{
				if(user.getRoleinfo().getRoleid()==1){
					slmslist=msbiz.getByTime(begintime, endtime);
				}else if(user.getRoleinfo().getRoleid()==2){
					slmslist=msbiz.getPgByTime(user, begintime, endtime);
				}else if(user.getRoleinfo().getRoleid()==3){
					slmslist=msbiz.getPuByTime(user, begintime, endtime);
				}
			}
			session.put("slmslist", slmslist);
			//response.sendRedirect("selectMain.jsp");
			
		}else if((begintime==null||begintime.length()==0)&&(endtime==null||endtime.length()==0)&&alltime!=null){
			
			List<Manualsign> slmslist=null;
			if("day".equals(alltime)){
				
				if(department!=null){
					if(user.getRoleinfo().getRoleid()==1){
						slmslist=msbiz.getByTimeAndDept(day+" 00:00:00", day+" 24:00:00", department);
					}else if(user.getRoleinfo().getRoleid()==2){
						slmslist=msbiz.getPgByTime(user, day+" 00:00:00", day+" 24:00:00");
					}else if(user.getRoleinfo().getRoleid()==3){
						slmslist=msbiz.getPuByTime(user, day+" 00:00:00", day+" 24:00:00");
					}
				}else if(userid!=0){
					
					slmslist=msbiz.getByIdAndTime(day+" 00:00:00", day+" 24:00:00", userid);
				}else if(username!=null){
					
					slmslist=msbiz.getByUnameAndTime(day+" 00:00:00", day+" 24:00:00", username);
				}else{
					if(user.getRoleinfo().getRoleid()==1){
						slmslist=msbiz.getByTime(day+" 00:00:00", day+" 24:00:00");
					}else if(user.getRoleinfo().getRoleid()==2){
						slmslist=msbiz.getPgByTime(user, day+" 00:00:00", day+" 24:00:00");
					}else if(user.getRoleinfo().getRoleid()==3){
						slmslist=msbiz.getPuByTime(user, day+" 00:00:00", day+" 24:00:00");
					}
				}
				
			}else if("mouth".equals(alltime)){
				if(department!=null){
					if(user.getRoleinfo().getRoleid()==1){
						slmslist=msbiz.getByTimeAndDept(month+"-01", month+"-31", department);
					}else if(user.getRoleinfo().getRoleid()==2){
						slmslist=msbiz.getPgByTime(user, month+"-01", month+"-31");
					}else if(user.getRoleinfo().getRoleid()==3){
						slmslist=msbiz.getPuByTime(user, month+"-01", month+"-31");
					}
					
				}else if(userid!=0){
					
					slmslist=msbiz.getByIdAndTime(month+"-01", month+"-31", userid);
				}else if(username!=null){
					
					slmslist=msbiz.getByUnameAndTime(month+"-01", month+"-31", username);
				}else{
					if(user.getRoleinfo().getRoleid()==1){
						slmslist=msbiz.getByTime(month+"-01", month+"-31");
					}else if(user.getRoleinfo().getRoleid()==2){
						slmslist=msbiz.getPgByTime(user, month+"-01", month+"-31");
					}else if(user.getRoleinfo().getRoleid()==3){
						slmslist=msbiz.getPuByTime(user, month+"-01", month+"-31");
					}
					 
				}
				
			}else if("year".equals(alltime)){
				if(department!=null){
					if(user.getRoleinfo().getRoleid()==1){
						slmslist=msbiz.getByTimeAndDept(year+"-01-01", year+"-12-31", department);
					}else if(user.getRoleinfo().getRoleid()==2){
						slmslist=msbiz.getPgByTime(user, year+"-01-01", year+"-12-31");
					}else if(user.getRoleinfo().getRoleid()==3){
						slmslist=msbiz.getPuByTime(user, year+"-01-01", year+"-12-31");
					}
					
				}else if(userid!=0){
					
					slmslist=msbiz.getByIdAndTime(year+"-01-01", year+"-12-31", userid);
				}else if(username!=null){
					
					slmslist=msbiz.getByUnameAndTime(year+"-01-01", year+"-12-31", username);
				}else{
					if(user.getRoleinfo().getRoleid()==1){
						slmslist=msbiz.getByTime(year+"-01-01", year+"-12-31");
					}else if(user.getRoleinfo().getRoleid()==2){
						slmslist=msbiz.getPgByTime(user, year+"-01-01", year+"-12-31");
					}else if(user.getRoleinfo().getRoleid()==3){
						slmslist=msbiz.getPuByTime(user, year+"-01-01", year+"-12-31");
					}
					
				}
				
			}else if("week".equals(alltime)){

				if(department!=null){
					if(user.getRoleinfo().getRoleid()==1){
						slmslist=msbiz.getByTimeAndDept(btime, etime, department);
					}else if(user.getRoleinfo().getRoleid()==2){
						slmslist=msbiz.getPgByTime(user, btime, etime);
					}else if(user.getRoleinfo().getRoleid()==3){
						slmslist=msbiz.getPuByTime(user, btime, etime);
					}
					
				}else if(userid!=0){
					
					slmslist=msbiz.getByIdAndTime(btime, etime, userid);
				}else if(username!=null){
					
					slmslist=msbiz.getByUnameAndTime(btime, etime, username);
				}else{
					if(user.getRoleinfo().getRoleid()==1){
						slmslist=msbiz.getByTime(btime, etime);
					}else if(user.getRoleinfo().getRoleid()==2){
						slmslist=msbiz.getPgByTime(user, btime, etime);
					}else if(user.getRoleinfo().getRoleid()==3){
						slmslist=msbiz.getPuByTime(user, btime, etime);
					}
					
				}
				
			
			}
			
			session.put("slmslist", slmslist);
			//response.sendRedirect("selectMain.jsp");
		}else if((begintime==null||begintime.length()==0)&&(endtime==null||endtime.length()==0)&&alltime==null){
			List<Manualsign> slmslist=null;
			
			if(department!=null){
				if(user.getRoleinfo().getRoleid()==1){
					slmslist=msbiz.getByTimeAndDept("1970-01-01",  day+" 24:00:00", department);
				}else if(user.getRoleinfo().getRoleid()==2){
					slmslist=msbiz.getPgByTime(user, "1970-01-01",  day+" 24:00:00");
				}else if(user.getRoleinfo().getRoleid()==3){
					slmslist=msbiz.getPuByTime(user, "1970-01-01", day+" 24:00:00");
				}
				
			}else if(userid!=0){
				
				slmslist=msbiz.getByIdAndTime("1970-01-01",day+" 24:00:00", userid);
			}else if(username!=null){
				
				slmslist=msbiz.getByUnameAndTime("1970-01-01", day+" 24:00:00", username);
			}else{
				if(user.getRoleinfo().getRoleid()==1){
					slmslist=msbiz.getByTime("1970-01-01", day+" 24:00:00");
				}else if(user.getRoleinfo().getRoleid()==2){
					slmslist=msbiz.getPgByTime(user, "1970-01-01", day+" 24:00:00");
				}else if(user.getRoleinfo().getRoleid()==3){
					slmslist=msbiz.getPuByTime(user, "1970-01-01", day+" 24:00:00");
				}
			}
			session.put("slmslist", slmslist);
			//response.sendRedirect("selectMain.jsp");
		}
		
		int type=2;
		session.put("type", type);
		return "success";
	}

	public void setSession(Map<String, Object> arg0) {
		this.session=arg0;
	}

	public String getBegintime() {
		return begintime;
	}

	public void setBegintime(String begintime) {
		this.begintime = begintime;
	}

	public String getEndtime() {
		return endtime;
	}

	public void setEndtime(String endtime) {
		this.endtime = endtime;
	}

	public String getAlltime() {
		return alltime;
	}

	public void setAlltime(String alltime) {
		this.alltime = alltime;
	}

	public String getRange() {
		return range;
	}

	public void setRange(String range) {
		this.range = range;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public ManualSignBiz getMsbiz() {
		return msbiz;
	}

	public void setMsbiz(ManualSignBiz msbiz) {
		this.msbiz = msbiz;
	}

	
	
}
